/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library.gui;

import com.jgoodies.looks.plastic.PlasticXPLookAndFeel;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.ConnectException;
import java.security.NoSuchAlgorithmException;
import javax.xml.ws.WebServiceException;
import library.gui.create.NewBookDialog;
import library.gui.create.NewUserDialog;
import library.gui.edit.EditBookDialog;
import library.gui.edit.EditUserDialog;
import library.gui.models.BookTableModel;
import library.gui.models.DepartmentBoxModel;
import library.gui.models.UserTableModel;
import library.gui.tools.Tools;
import library.webservice.Department;
import library.webservice.IllegalArgumentException_Exception;

/**
 *
 * @author Emptak,Andrej
 */
public class MainFrame extends javax.swing.JFrame {

    // private static final ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    private ConnectionHolder conn = null;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        myInit();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelBooks = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableBooks = new javax.swing.JTable();
        jLabelSearchByTitle = new javax.swing.JLabel();
        jLabelSearchByAuthor = new javax.swing.JLabel();
        jTextFieldBookTitle = new javax.swing.JTextField();
        jTextFieldBookAuthor = new javax.swing.JTextField();
        jButtonSearchByTitle = new javax.swing.JButton();
        jButtonSearchByAuthor = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButtonResetBooksTable = new javax.swing.JButton();
        jLabelFilterByDepartment = new javax.swing.JLabel();
        jButtonCreateBook = new javax.swing.JButton();
        jButtonEditBook = new javax.swing.JButton();
        jButtonDeleteBook = new javax.swing.JButton();
        jPanelUsers = new javax.swing.JPanel();
        jLabelRealName = new javax.swing.JLabel();
        jTextFieldSearchByRealName = new javax.swing.JTextField();
        jButtonUserSearch = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableUsers = new javax.swing.JTable();
        jButtonUserTableRefresh = new javax.swing.JButton();
        jButtonDeleteUser = new javax.swing.JButton();
        jButtonEditUser = new javax.swing.JButton();
        jButtonCreateUser = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("Messages"); // NOI18N
        setTitle(bundle.getString("gui.frame.title.main")); // NOI18N
        setMinimumSize(new java.awt.Dimension(169, 200));
        setPreferredSize(new java.awt.Dimension(800, 550));
        getContentPane().setLayout(new java.awt.CardLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jTextField1.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 53;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jTextField1, gridBagConstraints);

        jPasswordField1.setPreferredSize(new java.awt.Dimension(48, 20));
        jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPasswordField1KeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 105;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jPasswordField1, gridBagConstraints);

        jButton1.setText("Login");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(jButton1, gridBagConstraints);

        jLabel1.setText("Username");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel1.add(jLabel1, gridBagConstraints);

        jLabel2.setText("Password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel1.add(jLabel2, gridBagConstraints);

        jLabel3.setMaximumSize(new java.awt.Dimension(50, 400));
        jLabel3.setMinimumSize(new java.awt.Dimension(50, 400));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        jPanel1.add(jLabel3, gridBagConstraints);

        getContentPane().add(jPanel1, "card3");

        jTableBooks.setModel(new BookTableModel());
        jScrollPane1.setViewportView(jTableBooks);

        jLabelSearchByTitle.setText(bundle.getString("gui.frame.books.search.bytitle")); // NOI18N

        jLabelSearchByAuthor.setText(bundle.getString("gui.frame.books.search.byauthor")); // NOI18N

        jTextFieldBookTitle.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldBookTitleKeyPressed(evt);
            }
        });

        jTextFieldBookAuthor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldBookAuthorKeyPressed(evt);
            }
        });

        jButtonSearchByTitle.setText(bundle.getString("gui.frame.button.search")); // NOI18N
        jButtonSearchByTitle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchByTitleActionPerformed(evt);
            }
        });

        jButtonSearchByAuthor.setText(bundle.getString("gui.frame.button.search")); // NOI18N
        jButtonSearchByAuthor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchByAuthorActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new DepartmentBoxModel());
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButtonResetBooksTable.setText(bundle.getString("gui.frame.button.refresh")); // NOI18N
        jButtonResetBooksTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonResetBooksTableActionPerformed(evt);
            }
        });

        jLabelFilterByDepartment.setText(bundle.getString("gui.frame.books.filterby")); // NOI18N

        jButtonCreateBook.setText(bundle.getString("gui.frame.books.button.create")); // NOI18N
        jButtonCreateBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCreateBookActionPerformed(evt);
            }
        });

        jButtonEditBook.setText(bundle.getString("gui.frame.books.button.edit")); // NOI18N
        jButtonEditBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditBookActionPerformed(evt);
            }
        });

        jButtonDeleteBook.setText(bundle.getString("gui.frame.books.button.delete")); // NOI18N
        jButtonDeleteBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteBookActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelBooksLayout = new javax.swing.GroupLayout(jPanelBooks);
        jPanelBooks.setLayout(jPanelBooksLayout);
        jPanelBooksLayout.setHorizontalGroup(
            jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBooksLayout.createSequentialGroup()
                .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelBooksLayout.createSequentialGroup()
                        .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelBooksLayout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabelSearchByTitle)
                                .addGap(37, 37, 37))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBooksLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelSearchByAuthor)
                                    .addComponent(jLabelFilterByDepartment))
                                .addGap(18, 18, 18)))
                        .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldBookTitle)
                            .addComponent(jTextFieldBookAuthor)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonSearchByAuthor, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonSearchByTitle, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButtonEditBook, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(jButtonDeleteBook, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(jButtonCreateBook, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanelBooksLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
                            .addGroup(jPanelBooksLayout.createSequentialGroup()
                                .addComponent(jButtonResetBooksTable, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanelBooksLayout.setVerticalGroup(
            jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBooksLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelBooksLayout.createSequentialGroup()
                        .addComponent(jButtonCreateBook)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEditBook)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonDeleteBook))
                    .addGroup(jPanelBooksLayout.createSequentialGroup()
                        .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldBookTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonSearchByTitle)
                            .addComponent(jLabelSearchByTitle))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldBookAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonSearchByAuthor)
                            .addComponent(jLabelSearchByAuthor))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelBooksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelFilterByDepartment))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addComponent(jButtonResetBooksTable)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jTabbedPane1.addTab(bundle.getString("gui.frame.tabbed.books"), jPanelBooks); // NOI18N

        jLabelRealName.setText(bundle.getString("gui.user.realname")); // NOI18N

        jTextFieldSearchByRealName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldSearchByRealNameKeyPressed(evt);
            }
        });

        jButtonUserSearch.setText(bundle.getString("gui.frame.button.search")); // NOI18N
        jButtonUserSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUserSearchActionPerformed(evt);
            }
        });

        jTableUsers.setModel(new UserTableModel());
        jScrollPane2.setViewportView(jTableUsers);

        jButtonUserTableRefresh.setText(bundle.getString("gui.frame.button.refresh")); // NOI18N
        jButtonUserTableRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUserTableRefreshActionPerformed(evt);
            }
        });

        jButtonDeleteUser.setText(bundle.getString("gui.frame.user.button.user.delete")); // NOI18N
        jButtonDeleteUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteUserActionPerformed(evt);
            }
        });

        jButtonEditUser.setText(bundle.getString("gui.frame.user.button.user.edit")); // NOI18N
        jButtonEditUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditUserActionPerformed(evt);
            }
        });

        jButtonCreateUser.setText(bundle.getString("gui.frame.user.button.user.create")); // NOI18N
        jButtonCreateUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCreateUserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelUsersLayout = new javax.swing.GroupLayout(jPanelUsers);
        jPanelUsers.setLayout(jPanelUsersLayout);
        jPanelUsersLayout.setHorizontalGroup(
            jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUsersLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelUsersLayout.createSequentialGroup()
                        .addGroup(jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelUsersLayout.createSequentialGroup()
                                .addComponent(jLabelRealName)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldSearchByRealName, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonUserSearch))
                            .addComponent(jButtonUserTableRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButtonCreateUser, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(jButtonEditUser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonDeleteUser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanelUsersLayout.setVerticalGroup(
            jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUsersLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelRealName)
                    .addComponent(jTextFieldSearchByRealName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonUserSearch)
                    .addComponent(jButtonCreateUser))
                .addGap(9, 9, 9)
                .addComponent(jButtonEditUser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonUserTableRefresh)
                    .addComponent(jButtonDeleteUser))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab(bundle.getString("gui.frame.tabbed.users"), jPanelUsers); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 599, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 634, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                    .addComponent(jTabbedPane1)
                    .addContainerGap()))
        );

        getContentPane().add(jPanel2, "card2");

        jMenu1.setText("Logout");

        jMenuItem1.setText("Logout");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCreateBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCreateBookActionPerformed

        NewBookDialog newBook = new NewBookDialog(this, false);
        newBook.setReq(conn, getBTM());
        newBook.setVisible(true);

    }//GEN-LAST:event_jButtonCreateBookActionPerformed

    private void jButtonEditBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditBookActionPerformed
        int index = jTableBooks.getSelectedRow();

        if (index != -1) {
            EditBookDialog ebd = new EditBookDialog(this, false);
            try {
                ebd.setReq(getBTM().getBookAt(index), conn, getBTM());
            } catch (NullPointerException ex) {
                System.err.println(ex.getMessage());
            }

            ebd.setVisible(true);
        } else {
            Tools.createErrorDialog(java.util.ResourceBundle.getBundle("Messages").getString("gui.dialog.book.error"));
        }
    }//GEN-LAST:event_jButtonEditBookActionPerformed

    private void jButtonResetBooksTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResetBooksTableActionPerformed
        try {
            getBTM().addBooks(conn.getBws().getAllBooks());
        } catch (ConnectException | NullPointerException ex) {
            System.err.println(ex.getMessage());
        }

        jTextFieldBookAuthor.setText("");
        jTextFieldBookTitle.setText("");
    }//GEN-LAST:event_jButtonResetBooksTableActionPerformed

    private void jButtonDeleteBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteBookActionPerformed
        int index = jTableBooks.getSelectedRow();

        if (index != -1) {

            try {
                conn.getBws().deleteBook(getBTM().getBookAt(index));
                getBTM().addBooks(conn.getBws().getAllBooks());
                getBTM().refresh();
            } catch (ConnectException | NullPointerException | IllegalArgumentException_Exception | IllegalArgumentException ex) {
                System.err.println(ex.getMessage());
            }

        } else {
            Tools.createErrorDialog(java.util.ResourceBundle.getBundle("Messages").getString("gui.dialog.book.error"));
        }
    }//GEN-LAST:event_jButtonDeleteBookActionPerformed

    private void jButtonSearchByTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchByTitleActionPerformed
        try {
            getBTM().addBooks(conn.getBws().searchBooksByTitle(jTextFieldBookTitle.getText()));
        } catch (ConnectException | NullPointerException | IllegalArgumentException_Exception | IllegalArgumentException ex) {
            System.err.println(ex.getMessage());
        }
    }//GEN-LAST:event_jButtonSearchByTitleActionPerformed

    private void jButtonSearchByAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchByAuthorActionPerformed
        jButtonSearchByAuthor.doClick();
    }//GEN-LAST:event_jButtonSearchByAuthorActionPerformed

    private void jTextFieldBookTitleKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldBookTitleKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            jButtonSearchByTitle.doClick();
        }

    }//GEN-LAST:event_jTextFieldBookTitleKeyPressed

    private void jTextFieldBookAuthorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldBookAuthorKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            try {
                getBTM().addBooks(conn.getBws().getBooksByAuthor(jTextFieldBookAuthor.getText()));
            } catch (ConnectException | NullPointerException | IllegalArgumentException_Exception | IllegalArgumentException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }//GEN-LAST:event_jTextFieldBookAuthorKeyPressed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        try {
            getBTM().addBooks(conn.getBws().getBooksByDepartment(Department.valueOf(jComboBox1.getModel().getSelectedItem().toString())));
        } catch (ConnectException | NullPointerException | IllegalArgumentException_Exception | IllegalArgumentException ex) {
            System.err.println(ex.getMessage());
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButtonCreateUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCreateUserActionPerformed
        NewUserDialog newBook = new NewUserDialog(this, false);
        newBook.setReq(conn, getUTM());
        newBook.setVisible(true);
    }//GEN-LAST:event_jButtonCreateUserActionPerformed

    private void jButtonEditUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditUserActionPerformed
        int index = jTableUsers.getSelectedRow();

        if (index != -1) {
            EditUserDialog eud = new EditUserDialog(this, false);
            try {
                eud.setReq(getUTM().getUserAt(index), conn, getUTM());
            } catch (NullPointerException ex) {
                System.err.println(ex.getMessage());
            }

            eud.setVisible(true);
        } else {
            Tools.createErrorDialog(java.util.ResourceBundle.getBundle("Messages").getString("gui.dialog.user.error"));
        }
    }//GEN-LAST:event_jButtonEditUserActionPerformed

    private void jButtonUserTableRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUserTableRefreshActionPerformed

        try {
            getUTM().addUsers(conn.getUws().getUsers());
        } catch (ConnectException | NullPointerException ex) {
            System.err.println(ex.getMessage());
        }

        jTextFieldSearchByRealName.setText("");
    }//GEN-LAST:event_jButtonUserTableRefreshActionPerformed

    private void jButtonDeleteUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteUserActionPerformed
        int index = jTableUsers.getSelectedRow();

        if (index != -1) {

            try {
                conn.getUws().deleteUser(getUTM().getUserAt(index));
                getUTM().addUsers(conn.getUws().getUsers());
                getUTM().refresh();
            } catch (ConnectException | NullPointerException | IllegalArgumentException_Exception | IllegalArgumentException ex) {
                System.err.println(ex.getMessage());
            }

        } else {
            Tools.createErrorDialog(java.util.ResourceBundle.getBundle("Messages").getString("gui.dialog.user.error"));
        }
    }//GEN-LAST:event_jButtonDeleteUserActionPerformed

    private void jButtonUserSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUserSearchActionPerformed
        try {
            getUTM().addUsers(conn.getUws().findUserByRealName(jTextFieldSearchByRealName.getText()));
        } catch (ConnectException | NullPointerException | IllegalArgumentException_Exception | IllegalArgumentException ex) {
            System.err.println(ex.getMessage());
        }
    }//GEN-LAST:event_jButtonUserSearchActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        jLabel3.setText("");
        jLabel3.setForeground(Color.BLACK);
        new Thread(new Runnable() {
            @Override
            public void run() {
                initAuth();
            }
        }).start();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jPasswordField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordField1KeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            jButton1.doClick();
        }
    }//GEN-LAST:event_jPasswordField1KeyPressed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        ((CardLayout) getContentPane().getLayout()).show(getContentPane(), "card3");
        jMenu1.setEnabled(false);
        jLabel3.setText("");
        ConnectionHolder.resetConnection();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jTextFieldSearchByRealNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldSearchByRealNameKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) 
        {
            jButtonUserSearch.doClick();
        }
    }//GEN-LAST:event_jTextFieldSearchByRealNameKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            if (Tools.isWin()) {
                javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            } else {
                javax.swing.UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
            }

//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Motif".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonCreateBook;
    private javax.swing.JButton jButtonCreateUser;
    private javax.swing.JButton jButtonDeleteBook;
    private javax.swing.JButton jButtonDeleteUser;
    private javax.swing.JButton jButtonEditBook;
    private javax.swing.JButton jButtonEditUser;
    private javax.swing.JButton jButtonResetBooksTable;
    private javax.swing.JButton jButtonSearchByAuthor;
    private javax.swing.JButton jButtonSearchByTitle;
    private javax.swing.JButton jButtonUserSearch;
    private javax.swing.JButton jButtonUserTableRefresh;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelFilterByDepartment;
    private javax.swing.JLabel jLabelRealName;
    private javax.swing.JLabel jLabelSearchByAuthor;
    private javax.swing.JLabel jLabelSearchByTitle;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelBooks;
    private javax.swing.JPanel jPanelUsers;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTableBooks;
    private javax.swing.JTable jTableUsers;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextFieldBookAuthor;
    private javax.swing.JTextField jTextFieldBookTitle;
    private javax.swing.JTextField jTextFieldSearchByRealName;
    // End of variables declaration//GEN-END:variables

    private BookTableModel getBTM() {
        return (BookTableModel) jTableBooks.getModel();
    }

    private UserTableModel getUTM() {
        return (UserTableModel) jTableUsers.getModel();
    }

    private void myInit() {
        setLocationRelativeTo(null);
        jMenu1.setEnabled(false);
    }

    private void initAuth() {
        boolean connSucc = true;

        String userName = jTextField1.getText();
        String password = new String(jPasswordField1.getPassword());
        try {
            conn = ConnectionHolder.getInstance();
            jLabel3.setText(java.util.ResourceBundle.getBundle("Messages").getString("gui.login.set.credentials"));
            conn.setServiceCredentials(userName, Tools.SHA1(password));
        } catch (java.io.IOException | NoSuchAlgorithmException ex) {
            System.err.println(ex.getMessage());
            connSucc = false;
            jLabel3.setText(java.util.ResourceBundle.getBundle("Messages").getString("gui.login.connectionerror") + ex.getMessage());
        }
        if (connSucc) {
            try {
                jLabel3.setText(java.util.ResourceBundle.getBundle("Messages").getString("gui.login.getall"));
                getBTM().addBooks(conn.getBws().getAllBooks());
                getUTM().addUsers(conn.getUws().getUsers());
                ((CardLayout) getContentPane().getLayout()).show(getContentPane(), "card2");
                jMenu1.setEnabled(true);
            } catch (IOException | NullPointerException ex) {
                System.err.println(ex.getMessage());
                jLabel3.setText(java.util.ResourceBundle.getBundle("Messages").getString("gui.login.error") + ex.getMessage()); //java.util.ResourceBundle.getBundle("Messages").getString("gui.login.error")
            } catch (WebServiceException ex) {
                jLabel3.setForeground(Color.RED);
                jLabel3.setText(java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("Messages").getString("gui.login.weberror"), ex.getMessage()));
            }
            catch(Exception e)
            {
                System.err.println("="+e.getMessage());
            }
            catch(Throwable e)
            {
                System.err.println(e.getMessage());
            }
        }
    }
}
